package com.ljph.jfinal.dto;

import java.util.Date;

/**
 * 封装API 返回结果
 * Created by yuzhou on 16/9/2.
 */
public class ApiResult<T> {

  // 是否成功
  private boolean ok;
  // 失败时的错误信息
  private String error = null;
  // 成功时返回的附加数据
  private T data = null;

  public Object getErrorData() {
    return errorData;
  }

  public ApiResult setErrorData(Object errorData) {
    this.errorData = errorData;
    return this;
  }

  // 失败时除了错误信息之外的附加出错详细数据
  private Object errorData = null;


  public Date getTs() {
    return ts;
  }

  public ApiResult setTs() {
    this.ts = new Date();
    return this;
  }

  public ApiResult setTs(Date ts) {
    this.ts = ts;
    return this;
  }

  private Date ts = null;

  public ApiResult() {

  }

  public ApiResult(boolean ok) {
    this.ok = ok;
  }

  public ApiResult(boolean ok, T data) {
    this.ok = ok;
    this.data = data;
  }

  public ApiResult(boolean ok, String error) {
    this.ok = ok;
    this.error = error;
  }

  public ApiResult(boolean ok, String error, T data) {
    this.ok = ok;
    this.error = error;
    this.data = data;
  }

  public boolean isOk() {
    return ok;
  }

  public ApiResult setOk(boolean ok) {
    this.ok = ok;
    return this;
  }

  public String getError() {
    return error;
  }

  public ApiResult setError(String error) {
    this.error = error;
    return this;
  }

  public ApiResult setError(Exception e) {
    this.error = "数据处理出错:" + e.getMessage();
    return this;
  }

  public T getData() {
    return this.data;
  }

  public ApiResult setData(T data) {
    this.data = data;
    return this;
  }
}
